// pages/submit-install-report/index.js
import { toPrevPage } from "../../utils/util";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    report_day: "", // 日期
    report_type: "", // 类型
    report_type_query: "",
    report_type_label: "",
    user_id: "", // 姓名
    user_name: "",
    report_address: "", // 地址
    report_num: "", // 数量
    dateIsError: "",
    typeIsError: "",
    nameIsError: "",
    editId: "",
    addressIsError: "",
    numberIsError: "",
    product_name: "",
    productIsError: "",
    hide: false,
    typeColumns: [
      {
        value: "1",
        query: "4",
        label: "装饰公司安装量上报",
      },
      {
        value: "2",
        query: "5",
        label: "水电工安装量上报",
      },
      {
        value: "3",
        query: "6",
        label: "设计师安装量上报",
      },
      {
        value: "4",
        query: "8",
        label: "项目经理安装量上报",
      },
    ],
    nameColumns: [],
  },

  bindDateChange(event) {
    const { value } = event.detail;

    this.setData({
      report_day: value,
      dateIsError: value ? "" : "请选择时间",
    });
  },

  bindUserChange(event) {
    const { value } = event.detail;
    const { nameColumns, report_type } = this.data;

    if (!report_type) {
      wx.showToast({
        title: "请选择类型",
        icon: "none",
        duration: 2000,
      });

      return;
    }

    this.setData({
      user_id: nameColumns[value]["id"],
      user_name: nameColumns[value]["name"],
      nameIsError: value ? "" : "请选择用户",
    });
  },

  bindTypeChange(event) {
    const { value } = event.detail;
    const { typeColumns } = this.data;

    this.setData({
      report_type: typeColumns[value]["value"],
      report_type_label: typeColumns[value]["label"],
      report_type_query: typeColumns[value]["query"],
      user_id: "",
      user_name: "",
      typeIsError: value ? "" : "请选择类型",
    });

    this.getUser(typeColumns[value]["query"]);
  },

  handleAddressInput(event) {
    const value = event.detail;

    this.setData({
      addressIsError: value ? "" : "请输入位置",
    });
  },

  handleNumberInput(event) {
    const value = event.detail;

    this.setData({
      numberIsError: value ? "" : "请输入数量",
    });
  },

  handleProductNameInput(event) {
    const value = event.detail;

    this.setData({
      productIsError: value ? "" : "请输入产品名称",
    });
  },

  handleSumbit() {
    const {
      report_day,
      report_type,
      user_id,
      report_address,
      report_num,
      product_name,
      editId,
    } = this.data;

    if (!report_day) {
      this.setData({
        dateIsError: "请选择时间",
      });

      return;
    }

    if (!report_type) {
      this.setData({
        typeIsError: "请选择类型",
      });

      return;
    }

    if (!user_id) {
      this.setData({
        nameIsError: "请输入姓名",
      });

      return;
    }

    if (!report_address) {
      this.setData({
        addressIsError: "请输入地址",
      });

      return;
    }

    if (!product_name) {
      this.setData({
        productIsError: "请输入产品名称",
      });

      return;
    }

    if (!report_num) {
      this.setData({
        numberIsError: "请输入数量",
      });

      return;
    }

    const params = editId
      ? {
          id: editId,
          report_num,
          report_day,
          report_type,
          report_address,
          user_id,
          product_name,
        }
      : {
          report_num,
          report_day,
          report_type,
          report_address,
          user_id,
          product_name,
        };

    wx.ajax
      .post(
        editId ? "editDataReportById.action" : "addDataReport.action",
        params,
        true
      )
      .then((res) => {
        // console.log(res);
        if (res.success) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000,
          });

          const timer = setTimeout(() => {
            toPrevPage();

            clearTimeout(timer);
          }, 1000);
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000,
          });
        }
      });
  },

  getUser(type) {
    wx.ajax
      .post(
        "queryUserInfoList.action",
        {
          user_type: type,
        },
        false
      )
      .then((res) => {
        // console.log(res);
        if (res.success) {
          this.setData({
            nameColumns: res.result || [],
          });

          const { user_id } = this.data;

          if (user_id) {
            this.setData({
              user_name: res.result.find((item) => item.id == user_id)["name"],
            });
          }
        }
      });
  },

  getData() {
    const { editId, typeColumns } = this.data;

    const params = { id: editId };

    wx.ajax.post("findDataReportById.action", params, true).then((res) => {
      // console.log(res);
      if (res.success) {
        const result = res.result;

        this.setData({
          report_day: result.report_day,
          report_type: result.report_type,
          report_type_label: typeColumns.find(
            (item) => item.value === result.report_type
          )["label"],
          user_id: result.user_id,
          report_address: result.report_address,
          product_name: result.product_name,
          report_num: result.report_num,
        });

        const userQueryType = typeColumns.find(
          (item) => item.value === result.report_type
        )["query"];

        this.getUser(userQueryType);
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      // 编辑
      wx.setNavigationBarTitle({
        title: "编辑上报",
      });

      this.setData({
        editId: options.id,
      });

      this.getData();

      this.setData({
        hide: options.hide == "1",
      });
    } else {
      wx.setNavigationBarTitle({
        title: "新增上报",
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
